/** 
 * https://leetcode.cn/problems/longest-substring-without-repeating-characters/submissions/560558905/
 * 03. 无重复字符的最长子串
 * medium,  2024.9.2
 * 滑动窗口
 */

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<char,int> map; 
        int ans=0;      
        int begin=0;
        int end=0;
        for(int end=0; end<s.length();end++){
            if(!map.count(s[end])||map[s[end]]==-1){ 
                ans=max(ans,end-begin+1); 
            }
            else{ 
                while(begin<=map[s[end]]){
                    map[s[begin++]] = -1; 
                }   
            }
            map[s[end]]=end; 
        }
        return ans;
    }
};